/**
 * 
 */
package com.heer.RD.projecthour.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.Assert;

import com.heer.RD.projecthour.pojo.ProjectDTO;
import com.heer.RD.projecthour.pojo.UserInfoDTO;
import com.heer.RD.util.SecurityFacade;
import com.heer.joss.persistence.IBaseDAO;

/**
 * @author ZhaoZehui
 *
 */
public class ProjHourService {
	
	private String projCostStatSql;
	private String projUserCostSql;
	private String userProjCostStatSql;
	

	public String getProjCostStatSql() {
		return projCostStatSql;
	}

	public void setProjCostStatSql(String projCostStatSql) {
		this.projCostStatSql = projCostStatSql;
	}

	public String getProjUserCostSql() {
		return projUserCostSql;
	}

	public void setProjUserCostSql(String projUserCostSql) {
		this.projUserCostSql = projUserCostSql;
	}


	public String getUserProjCostStatSql() {
		return userProjCostStatSql;
	}

	public void setUserProjCostStatSql(String userProjCostStatSql) {
		this.userProjCostStatSql = userProjCostStatSql;
	}


	private JdbcTemplate jdbcTemplate;
	
	private IBaseDAO userDao;
	
	private IBaseDAO projDao;
	
	private IBaseDAO userHourDao;
	
	private IBaseDAO userProjDao;
	
	private IBaseDAO deptDao;
	
	private IBaseDAO userDeptDao;
	
	private IBaseDAO projMilestoneDao;
	
	public IBaseDAO getUserDao() {
		return userDao;
	}

	public void setUserDao(IBaseDAO userDao) {
		this.userDao = userDao;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public IBaseDAO getProjDao() {
		return projDao;
	}

	public void setProjDao(IBaseDAO projDao) {
		this.projDao = projDao;
	}

	public IBaseDAO getUserHourDao() {
		return userHourDao;
	}

	public void setUserHourDao(IBaseDAO userHourDao) {
		this.userHourDao = userHourDao;
	}

	public IBaseDAO getUserProjDao() {
		return userProjDao;
	}

	public void setUserProjDao(IBaseDAO userProjDao) {
		this.userProjDao = userProjDao;
	}

	public IBaseDAO getDeptDao() {
		return deptDao;
	}

	public void setDeptDao(IBaseDAO deptDao) {
		this.deptDao = deptDao;
	}

	public IBaseDAO getUserDeptDao() {
		return userDeptDao;
	}

	public void setUserDeptDao(IBaseDAO userDeptDao) {
		this.userDeptDao = userDeptDao;
	}

	public IBaseDAO getProjMilestoneDao() {
		return projMilestoneDao;
	}

	public void setProjMilestoneDao(IBaseDAO projMilestoneDao) {
		this.projMilestoneDao = projMilestoneDao;
	}
	
	
	public ProjectDTO getProjById(String projId) {
		return (ProjectDTO)projDao.findById(projId);
	}
	
	public String getDeptName(String deptId) {
		String sql = "select dept_name from department where dept_id = ?";
		return (String)jdbcTemplate.queryForObject(sql, new Object[]{deptId}, String.class);
	}
	
	public String getFatherId(String deptId) {
		String sql = "select father_id from department where dept_id = ?";
		return (String)jdbcTemplate.queryForObject(sql, new Object[]{deptId}, String.class);
	}
	
	/**
	 * 得到第一级部门（含）及下面的所有部门
	 * @param jdbcTemplate
	 * @param deptId
	 * @return
	 */
	public String[] getLevel1AndSubDepts(String deptId) {
		List<String> deptIdList = new ArrayList<String>();
		
		if("root".equals(deptId)) {
			return getSubDepts(deptId);
		}
		else {
			String sql = "select father_id from department where dept_id = ?";
			String fatherId1 = (String)jdbcTemplate.queryForObject(sql, new Object[]{deptId}, String.class);
			if("root".equals(fatherId1)) {
				return getSubDepts(deptId);
			}
			else {
				String fatherId2 = (String)jdbcTemplate.queryForObject(sql, new Object[]{fatherId1}, String.class);
				if("root".equals(fatherId2)) {
					return getSubDepts(fatherId1);
				}
				else {
					String fatherId3 = (String)jdbcTemplate.queryForObject(sql, new Object[]{fatherId2}, String.class);
					if("root".equals(fatherId3)) {
						return getSubDepts(fatherId2);
					}
				}
			}
		}
		return deptIdList.toArray(new String[]{});
	}
	
	/**
	 * 得到当前部门下面的所有部门id，拼成字符数组
	 * @param jdbcTemplate
	 * @param deptId
	 * @return
	 */
	public String[] getSubDepts(String deptId) {
		List<String> deptIdList = new ArrayList<String>();
		
		deptIdList.add(deptId);
		
		String sql = "select dept_id from department where father_id = ? order by dept_name";
		List<Map<String, String>> level1List = jdbcTemplate.queryForList(sql, new Object[]{deptId});
		for (Map<String, String> dept1 : level1List) {
			deptIdList.add(dept1.get("DEPT_ID"));
			
			List<Map<String, String>> level2List = jdbcTemplate.queryForList(sql, new Object[]{dept1.get("DEPT_ID")});
			for (Map<String, String> dept2 : level2List) {
				deptIdList.add(dept2.get("DEPT_ID"));
				
				List<Map<String, String>> level3List = jdbcTemplate.queryForList(sql, new Object[]{dept2.get("DEPT_ID")});
				for (Map<String, String> dept3 : level3List) {
					deptIdList.add(dept3.get("DEPT_ID"));
					
					List<Map<String, String>> level4List = jdbcTemplate.queryForList(sql, new Object[]{dept3.get("DEPT_ID")});
					for (Map<String, String> dept4 : level4List) {
						deptIdList.add(dept4.get("DEPT_ID"));
						
					}
				}
			}
		}
		
		return deptIdList.toArray(new String[]{});
	}
	
	public String getProjName(String projId) {
		String sql = "select proj_name from project where project_id = ?";
		return (String)jdbcTemplate.queryForObject(sql, new Object[]{projId}, String.class);
	}
	
	public Map<String, String> getDeptAndProjs(UserInfoDTO user, boolean displayLevel1) {
		Assert.notNull(user);
		
		Map<String, String> result = new LinkedHashMap<String, String>();
		
		//找项目展示的起始部门id
		String startDeptId = user.getDeptId();
		
		//登记工时，寻找自己所在大部门的部门id
		if(displayLevel1) {
			if("root".equals(startDeptId)) {
				//do nothing
			}
			else {
				String sql = "select father_id from department where dept_id = ?";
				String fatherId1 = (String)jdbcTemplate.queryForObject(sql, new Object[]{startDeptId}, String.class);
				if("root".equals(fatherId1)) {
					//do nothing
				}
				else {
					String fatherId2 = (String)jdbcTemplate.queryForObject(sql, new Object[]{fatherId1}, String.class);
					if("root".equals(fatherId2)) {
						startDeptId = fatherId1;
					}
					else {
						String fatherId3 = (String)jdbcTemplate.queryForObject(sql, new Object[]{fatherId2}, String.class);
						if("root".equals(fatherId3)) {
							startDeptId = fatherId2;
						}
					}
				}
			}
		}
		//项目工时统计，查询自己作为项目经理的项目，和自己部门的项目
		else {
			if(SecurityFacade.isPM()) {
				result.put("dept_own_projects", "以下是本人负责的项目：");
				
				String sql = "select p.project_id,p.proj_name from project p,user_proj up where p.project_id = up.project_id and up.user_id = ? order by begin_date desc";
				List ownerProjList = jdbcTemplate.queryForList(sql, new Object[]{user.getUserId()});
				for (Iterator iterator = ownerProjList.iterator(); iterator
						.hasNext();) {
					Map pMap = (Map) iterator.next();
					result.put("P"+(String)pMap.get("PROJECT_ID"), "├…"+(String)pMap.get("PROJ_NAME"));
				}
			}
			
			if(SecurityFacade.isManager()) {
				result.put("dept_own_dept", "以下是部门项目：");
			}
			else {
				startDeptId = "not_exists";
			}
		}
		
		String osql = "select dept_id,dept_name from department where dept_id = ?";
		String sql = "select dept_id,dept_name from department where father_id = ? order by dept_name";
		
		List<Map<String, String>> level1List = jdbcTemplate.queryForList(osql, new Object[]{startDeptId});
		for (Map<String, String> dept1 : level1List) {
			result.put("dept_"+dept1.get("DEPT_ID"), dept1.get("DEPT_NAME"));
			
			queryProjAndSubProjList(result, "├…", dept1.get("DEPT_ID"));
			
			List<Map<String, String>> level2List = jdbcTemplate.queryForList(sql, new Object[]{dept1.get("DEPT_ID")});
			for (Map<String, String> dept2 : level2List) {
				result.put("dept_"+dept2.get("DEPT_ID"), "├─"+dept2.get("DEPT_NAME"));
				
				queryProjAndSubProjList(result, "├─…", dept2.get("DEPT_ID"));
				
				List<Map<String, String>> level3List = jdbcTemplate.queryForList(sql, new Object[]{dept2.get("DEPT_ID")});
				for (Map<String, String> dept3 : level3List) {
					result.put("dept_"+dept3.get("DEPT_ID"), "├─├─"+dept3.get("DEPT_NAME"));
					
					queryProjAndSubProjList(result, "├─├─…", dept3.get("DEPT_ID"));
					
					List<Map<String, String>> level4List = jdbcTemplate.queryForList(sql, new Object[]{dept3.get("DEPT_ID")});
					for (Map<String, String> dept4 : level4List) {
						result.put("dept_"+dept4.get("DEPT_ID"), "├─├─├─"+dept4.get("DEPT_NAME"));
						
						queryProjAndSubProjList(result, "├─├─├─…", dept4.get("DEPT_ID"));
					}
				}
			}
		}
		
		return result;
		
	}
	
	public void queryProjAndSubProjList(Map<String, String> result, String prefx, String deptId) {
		List<ProjectDTO> pList = projDao.queryByCriteria("dept_id = '"+deptId+"' and father_proid is null order by begin_date desc");
		for (ProjectDTO proj : pList) {
			result.put("D"+proj.getProjectId(), prefx + proj.getProjName());
			
			List<ProjectDTO> spList = projDao.queryByCriteria("father_proid = '"+proj.getProjectId()+"' order by begin_date desc");
			for (ProjectDTO sproj : spList) {
				result.put("D"+sproj.getProjectId(), prefx + "…" + sproj.getProjName());
				
			}
		}
	}
	
}

